package sluentities;

import java.util.ArrayList;
import java.util.List;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;
import org.mongodb.morphia.query.Query;
import config.MongoDB;


@Entity(value = "slots", noClassnameStored = true)
public class GrammarSlotEntity {

    @Id
    protected ObjectId id;
    protected String name;
    protected List<String> lists = new ArrayList<String>();

    public GrammarSlotEntity() {
        super();
    }

    public void setId(ObjectId id) {
        this.id = id;
    }

    public void setLists(List<String> lists) {
        this.lists = lists;
    }

    public ObjectId getId() {
        return id;
    }

    public List<String> getLists() {
        return lists;
    }
    
    public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public GrammarSlotEntity(String name) {
		super();
		this.name = name;
	}

	public GrammarSlotEntity(ObjectId id) {
		super();
		this.id = id;
	}

	public GrammarSlotEntity(ObjectId id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public GrammarSlotEntity remove(){

        Datastore mongoDatastore = MongoDB.instance().getDatabase();

        Query<GrammarRuleEntity> q = mongoDatastore.createQuery(GrammarRuleEntity.class);
        for (GrammarRuleEntity rule:q){
            boolean del=false;
            for (RuleEntryEntity entry:rule.getLists()){
                if(entry.getEntry().equals(id)){
                    del=true;
                    break;
                }
            }
            if(del)mongoDatastore.delete(rule.remove());
        }
        return this;
    }
}
